package Editeur;

import java.awt.ComponentOrientation;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;

import javax.swing.JPanel;

import Impl.ImageBoulder;
import Impl.Terrain;
import Impl.Type;
import Services.TerrainService;

public class TerrainPanel extends JPanel implements MouseMotionListener, MouseListener{
	private static final long serialVersionUID = 1L;
	private TerrainService oldTerrain;
	private TerrainService newTerrain;
	private ImageBoulder[][] images;
	private EditeurGraph editeur;
	private int nbDiamant = 0;
	private int nbPorte = 0;
	private int nbHero = 1;
	private int offsetX = 0;
	private int offsetY = 0;
	private int xdecal = 0;
	private int ydecal = 0;
	
	
	public TerrainPanel(int sizex, int sizey, TerrainService terrain, EditeurGraph editeur){
		setLayout(new GridLayout(sizey, sizex));
		applyComponentOrientation( ComponentOrientation.LEFT_TO_RIGHT); 
		this.editeur = editeur;
		
		
		oldTerrain = terrain;
		
		images = new ImageBoulder[sizex][sizey];
		for(int y=1; y<=sizey; y++)
			for(int x=1; x<=sizex; x++){
				images[x-1][y-1] = new ImageBoulder(x-1,y-1);
				images[x-1][y-1].addMouseMotionListener(this);
				images[x-1][y-1].addMouseListener(this);
			}
		
		
		newTerrain = new Terrain();
		newTerrain.init(sizex, sizey);
		
		for(int y=1; y<=sizey; y++){
			for(int x=1; x<=sizex; x++){
				if(x<=oldTerrain.getSizeX() && y<=oldTerrain.getSizeY()){
					newTerrain.getBloc(x, y).ChangeType(oldTerrain.getTypeBloc(x, y));
					images[x-1][y-1].setImage(oldTerrain.getTypeBloc(x, y));
				}
				this.add(images[x-1][y-1]);
			}
		}
	}
	
	public void modify(int sizex, int sizey){
		removeAll();
		setLayout(new GridLayout(sizey, sizex));
		applyComponentOrientation( ComponentOrientation.LEFT_TO_RIGHT); 
		
		oldTerrain = newTerrain;
		newTerrain = new Terrain();
		newTerrain.init(sizex, sizey);
		
		images = new ImageBoulder[sizex][sizey];
		for(int y=1; y<=sizey; y++)
			for(int x=1; x<=sizex; x++){
				images[x-1][y-1] = new ImageBoulder(x-1,y-1);
				images[x-1][y-1].addMouseMotionListener(this);
				images[x-1][y-1].addMouseListener(this);
			}
		
		for(int y=1; y<=sizey; y++){
			for(int x=1; x<=sizex; x++){
				if(x<=oldTerrain.getSizeX() && y<=oldTerrain.getSizeY()){
					newTerrain.getBloc(x, y).ChangeType(oldTerrain.getTypeBloc(x, y));
					images[x-1][y-1].setImage(oldTerrain.getTypeBloc(x, y));
				}
				this.add(images[x-1][y-1]);
			}
		}
		
		offsetX = 0;
		xdecal = 0;
		offsetY = 0;
		ydecal = 0;
		validate();
		repaint();
	}
	
	public TerrainService getTerrain(){
		return newTerrain;
	}
	
	@Override
	public void paint(Graphics g) {
		for(int x = 1; x<=newTerrain.getSizeX(); x++){
			for(int y = 1; y<=newTerrain.getSizeY(); y++){
				//images[x-1][y-1].setImage(newTerrain.getTypeBloc(x, y));
				images[x-1][y-1].setSizeX(getWidth()/newTerrain.getSizeX());
				images[x-1][y-1].setSizeY(getHeight()/newTerrain.getSizeY());
			}
		}
		super.paint(g);
	}

	@Override
	public void mouseDragged(MouseEvent me) {
		me.translatePoint(offsetX, offsetY);

		int x = me.getX();
		int y = me.getY();
		int sizeXImage = images[0][0].getSizeX();
		int sizeYImage = images[0][0].getSizeY();
		ImageBoulder img = ((ImageBoulder)me.getSource());
		
		if(x>sizeXImage){
			//me.translatePoint(-sizeXImage, 0);
			offsetX -= sizeXImage;
			xdecal++;
			me.setSource(images[img.getPositionX()+xdecal][img.getPositionY()+ydecal]);
		}
		else if(x<0){
			//me.translatePoint(sizeXImage, 0);
			offsetX += sizeXImage;
			xdecal--;
			me.setSource(images[img.getPositionX()+xdecal][img.getPositionY()+ydecal]);
		}
		if(y>sizeYImage){
			//me.translatePoint(0, -sizeYImage);
			offsetY -= sizeYImage;
			ydecal++;
			me.setSource(images[img.getPositionX()+xdecal][img.getPositionY()+ydecal]);
		}
		else if(y<0){
			//me.translatePoint(0, sizeYImage);
			offsetY += sizeYImage;
			ydecal--;
			me.setSource(images[img.getPositionX()+xdecal][img.getPositionY()+ydecal]);
		}
		
		img = ((ImageBoulder)me.getSource());
		
		if(editeur.getChoice()==Type.SortieFermee && img.getType()!=Type.SortieFermee){
			nbPorte++;
			editeur.MAJ(nbHero, nbPorte, nbDiamant);
		}
		else if(img.getType()==Type.SortieFermee && editeur.getChoice()!=Type.SortieFermee){
			nbPorte--;
			editeur.MAJ(nbHero, nbPorte, nbDiamant);
		}
		if(editeur.getChoice()==Type.Diamant && img.getType()!=Type.Diamant){
			nbDiamant++;
			editeur.MAJ(nbHero, nbPorte, nbDiamant);
		}
		else if(img.getType()==Type.Diamant && editeur.getChoice()!=Type.Diamant){
			nbDiamant--;
			editeur.MAJ(nbHero, nbPorte, nbDiamant);
		}
		if(editeur.getChoice()==Type.Hero && img.getType()!=Type.Hero){
			nbHero++;
			editeur.MAJ(nbHero, nbPorte, nbDiamant);
		}
		else if(img.getType()==Type.Hero && editeur.getChoice()!=Type.Hero){
			nbHero--;
			editeur.MAJ(nbHero, nbPorte, nbDiamant);
		}
		
		img.setImage(editeur.getChoice());
		validate();
		repaint();
	}

	@Override
	public void mouseMoved(MouseEvent me) {
	}
	
	public void save(String nameFile){
		try {
			int sizeX = newTerrain.getSizeX();
			int sizeY = newTerrain.getSizeY();
			FileWriter fw = new FileWriter(nameFile);
			BufferedWriter output = new BufferedWriter(fw);
			output.write(sizeX+" "+sizeY);
			output.newLine();
			
			for(int y=0; y<sizeY; y++){
				for(int x=0; x<sizeX; x++){
					output.write(images[x][y].getType().toString()+" ");
				}
				output.newLine();
			}
			
			output.flush();
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public void mouseClicked(MouseEvent me) {	
		ImageBoulder img;
		img = ((ImageBoulder)me.getSource());
		
		
		if(editeur.getChoice()==Type.SortieFermee && img.getType()!=Type.SortieFermee){
			nbPorte++;
			editeur.MAJ(nbHero, nbPorte, nbDiamant);
		}
		else if(img.getType()==Type.SortieFermee && editeur.getChoice()!=Type.SortieFermee){
			nbPorte--;
			editeur.MAJ(nbHero, nbPorte, nbDiamant);
		}
		if(editeur.getChoice()==Type.Diamant && img.getType()!=Type.Diamant){
			nbDiamant++;
			editeur.MAJ(nbHero, nbPorte, nbDiamant);
		}
		else if(img.getType()==Type.Diamant && editeur.getChoice()!=Type.Diamant){
			nbDiamant--;
			editeur.MAJ(nbHero, nbPorte, nbDiamant);
		}
		if(editeur.getChoice()==Type.Hero && img.getType()!=Type.Hero){
			nbHero++;
			editeur.MAJ(nbHero, nbPorte, nbDiamant);
		}
		else if(img.getType()==Type.Hero && editeur.getChoice()!=Type.Hero){
			nbHero--;
			editeur.MAJ(nbHero, nbPorte, nbDiamant);
		}
		
		img.setImage(editeur.getChoice());
		newTerrain.getBloc(img.getPositionX()+1,img.getPositionY()+1 ).ChangeType(editeur.getChoice());
		validate();
		repaint();
		offsetX = 0;
		xdecal = 0;
		offsetY = 0;
		ydecal = 0;
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		offsetX = 0;
		xdecal = 0;
		offsetY = 0;
		ydecal = 0;
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		offsetX = 0;
		xdecal = 0;
		offsetY = 0;
		ydecal = 0;		
	}
	
}
